package cn.adair.wechat;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

public class WeChatCallbackActivity extends AppCompatActivity implements IWXAPIEventHandler {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.transparent);
        if (WeChat.instance() != null) {
            WeChat.instance().iWxApi().handleIntent(getIntent(), this);
        } else {
            finish();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        if (WeChat.instance() != null) {
            WeChat.instance().iWxApi().handleIntent(intent, this);
        }
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.errStr != null) {
            Log.e("OxWeChat", "errstr=" + baseResp.errStr);
        }
        if (null == WeChat.instance()) {
            Log.e("WeChat", "请优先对WeChat进行初始化。Mathods is WeChat.init()");
            return;
        }
        WeChat.instance().onWeChatResponse(baseResp);
        finish();
    }
}
